package com.demo.juc.blockingqueue;

import org.junit.Test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @Author: jxm
 * @Description: 阻塞队列
 * @Date: 2024-2-6 10:34
 * @Version: 1.0
 */
public class ArrayBlockingQueueDemo {

    /**
     * 抛出异常
     */
    @Test
    public void test1() {
        //队列的大小
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue<>(3);
        System.out.println(arrayBlockingQueue.add("A"));
        System.out.println(arrayBlockingQueue.add("B"));
        System.out.println(arrayBlockingQueue.add("C"));
        //IllegalStateException 抛出异常
//        System.out.println(arrayBlockingQueue.add("D"));
        System.out.println("---------------");
        System.out.println(arrayBlockingQueue.element());//查看队首的元素
        System.out.println("---------------");
        System.out.println(arrayBlockingQueue.remove());//谁先进谁先出
        System.out.println(arrayBlockingQueue.remove());
        System.out.println(arrayBlockingQueue.remove());
        //NoSuchElementException
        //System.out.println(arrayBlockingQueue.remove());
    }

    /**
     * 不抛出异常
     */
    @Test
    public void test2() {
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
        System.out.println(arrayBlockingQueue.offer("A"));
        System.out.println(arrayBlockingQueue.offer("B"));
        System.out.println(arrayBlockingQueue.offer("C"));//true
        System.out.println("-----------------");
        System.out.println(arrayBlockingQueue.peek());
        System.out.println(arrayBlockingQueue.offer("D"));//false 不抛出异常 返回一个boolean值
        System.out.println("-----------------");
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());//依旧是先进先出
        System.out.println(arrayBlockingQueue.poll());//None 也不抛出异常
    }

    /**
     * 等待，阻塞（一直阻塞）
     */
    @Test
    public void test3() throws InterruptedException {
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
        //一直阻塞
        arrayBlockingQueue.put("A");
        arrayBlockingQueue.put("B");
        arrayBlockingQueue.put("C");
//        arrayBlockingQueue.put("D");//队列没有位置了，他会一直等待
        System.out.println("----------------");
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
        System.out.println(arrayBlockingQueue.take());
//        System.out.println(arrayBlockingQueue.take());
    }

    /**
     * 等待，（超时）
     */
    @Test
    public void test4() throws InterruptedException {
        ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(3);
        arrayBlockingQueue.offer("a");
        arrayBlockingQueue.offer("b");
        arrayBlockingQueue.offer("c");
        //arrayBlockingQueue.offer("d", 2,TimeUnit.SECONDS);//超时等待两秒
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll(2, TimeUnit.SECONDS));

    }


}
